package me.thinkjet.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import me.thinkjet.auth.AuthManager;
import me.thinkjet.auth.AuthRoles;
import me.thinkjet.interceptor.JobInterceptor;
import me.thinkjet.interceptor.JobRecordInterceptor;
import me.thinkjet.model.Area;
import me.thinkjet.model.Job;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.plugin.sqlinxml.SqlKit;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;
import me.thinkjet.model.JobRecord;
import me.thinkjet.model.JobType;
import me.thinkjet.service.AreaService;

@ControllerBind(controllerKey = "/job", viewPath = "job")
//@Before(JobInterceptor.class)
public class JobController extends Controller {

	public void index() {
	}

	@Before(CacheInterceptor.class)
	@CacheName("job-list")
	public void list() {
		int page = getParaToInt("page", 1);
		long area = getParaToInt("area", 0);
		int type = getParaToInt("type", 0);
		String q = getPara("q", "");
		String where = "where 1=1";
		if (area > 0) {
			String array = "(" + area;
			for (Area a : AreaService.getAllChildrens(area)) {
				array += "," + a.getLong("id");
			}
			array += ")";
			where += " and Job.area in " + array;
		}
		if (type > 0) {
			where += " and Job.type=" + type;
		}
		if (!q.equals("")) {
			where += " and Job.title LIKE '" + q + "'";
		}
		setAttr("job_list", Job.dao.paginateByCache("job",
				"job_" + page + "_" + where, page, 10,
				"select Job.*,Firm.name as firm_name,Record.*,Type.name as type_name,Area.name as area_name ",
				"from job Job left join firm Firm on Job.firm=Firm.id " +
						"left join job_record Record on Record.id=Job.id " +
						"left join job_type Type on Type.id=Job.type " +
						"left join area Area on Area.id=Job.area " +
						where + " order by Job.id desc"));
	}

	/**
	 *
	 */
	public void findByUser() {
		setAttr("joblist",
				Job.dao.find(SqlKit.sql("job.findByUser"), AuthManager
						.getSessionUser(this).getUser().getLong("id")));
		render("job-user.ftl");
	}

	@AuthRoles()
	public void add() {
	}

	// 提交发布招聘
	@AuthRoles()
	public void create() {
		Job job = getModel(Job.class);
		job.set("author", AuthManager.getSessionUser(this).getUser().getLong("id"));
		job.save();
		JobRecord.dao.set("id", job.getLong("id")).save();
		redirect("/job");
	}

	public void update() {
		Job job = getModel(Job.class);
		job.update();
		redirect("/job/show?id=" + job.get("id"));
	}

	@Before(JobRecordInterceptor.class)
	public void show() {
		setAttr("job", Job.dao.findFirst("select Job.*,Firm.name as firm_name,Record.*," +
				"Type.name as type_name,Area.name as area_name,User.name as uname," +
				"User.username,User.avatar from job Job " +
				"left join firm Firm on Job.firm=Firm.id " +
				"left join job_record Record on Record.id=Job.id " +
				"left join job_type Type on Type.id=Job.type " +
				"left join area Area on Area.id=Job.area " +
				"left join users User on User.id=Job.author " +
				"where Job.id=?", getParaToLong()));
	}

	/**
	 * 获取岗位分类
	 */
	public void type() {
		this.setAttr("type", JobType.dao.findByCache("job-type", "type", "select * from job_type"));
		renderJson();
	}

	public void hot() {
		setAttr("hot", Job.dao.findByCache("job", "job-hot",
				"select * from job where id in " +
						"(select id from " +
						"(select id from job_record order by views desc limit 0,5) as r" +
						")"));
	}
}
